/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelLateral.java
 *
 * Created on Jun 1, 2011, 6:09:22 PM
 */
package cataletra.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.font.TextAttribute;
import java.util.ArrayList;
import java.util.Map;
import javax.swing.JLabel;
import javax.swing.Timer;

/**
 *
 * @author arthur
 */
public class PainelLateral extends javax.swing.JPanel {

    private ArrayList<JLabel> labels;
    private int qtdePalavras;
    private int qtdePalavrasAchadas;
    private int nivel;
    
    String tema;
    String[] palavras;
    
    Timer timer;
    
    /** Creates new form PainelLateral */
    public PainelLateral() {
        initComponents();
    }

    public PainelLateral(String tema, String[] palavras) { 
        this.tema = tema;  
        inicializar(palavras);        
        this.labelPunicao.setText("+10");     
    }
    
    
    public PainelLateral(int nivel, String[] palavras) {
        this.nivel = nivel;
        this.tema = "Nível " + nivel;
        inicializar(palavras);        
        this.labelPunicao.setText("-10");
    }
    
    private void inicializar(String[] palavras){
        initComponents();
        
        qtdePalavras = palavras.length;
        this.palavras = palavras;
        
        this.setPalavras(this.palavras);
        
        ActionListener action = new ActionListener() { 
            int t = 0;
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(t <= 5){ labelPunicao.setVisible(!labelPunicao.isVisible()); }
                else{ timer.stop(); t = -1; } t++;} };        
        this.timer = new Timer(200, action);
        
        this.labelPunicao.setVisible(false);
    }

    public void punir(){
        this.timer.start();
    }
    
    public int getNivel() {
        return nivel;
    }

    public void setNivel(int nivel) {
        this.nivel = nivel;
    }    
        
    
    public final void setPalavras(String[] palavras){
        qtdePalavrasAchadas = 0;
        
        int linhas;
        if(qtdePalavras % 2 == 0){            //se a qtde de palavras for par, a metade é inteira
            linhas = qtdePalavras / 2;
        } else {                                 //se for impar, arredonde a metade pra cima.
            linhas = (qtdePalavras / 2) + 1;
        }
        
        GridLayout gridLayout = new GridLayout(linhas, 2, 10, 15);

        painelPalavras.removeAll();
        painelPalavras.setLayout(gridLayout);
        Font fonte = new Font("Tahoma", Font.PLAIN, 12);

        labels = new ArrayList();
        for(int i = 0; i < qtdePalavras; i++){
            JLabel label = new JLabel(palavras[i].toUpperCase());
            label.setFont(fonte);
            label.setHorizontalAlignment(JLabel.CENTER);
            labels.add(label);
            painelPalavras.add(label);
        }
        
        labelTema.setText(tema);
        labelContador.setText("Você achou 0 palavras de " + qtdePalavras);
    }
    
    public final void reiniciar(){
        this.setPalavras(palavras);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelTema = new javax.swing.JLabel();
        labelContador = new javax.swing.JLabel();
        painelPalavras = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        labelGambiarra = new javax.swing.JLabel();
        labelPunicao = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        labelTema.setFont(new java.awt.Font("Dialog", 1, 24));
        labelTema.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labelTema.setText("Título Tema");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 10, 5);
        add(labelTema, gridBagConstraints);

        labelContador.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelContador.setText("Nenhuma palavra achada");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        add(labelContador, gridBagConstraints);

        painelPalavras.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        painelPalavras.setLayout(new java.awt.GridLayout(5, 2, 10, 5));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("teste");
        painelPalavras.add(jLabel3);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("jLabel4");
        painelPalavras.add(jLabel4);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("teeeeste");
        painelPalavras.add(jLabel1);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("jLabel5");
        painelPalavras.add(jLabel5);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("teste");
        painelPalavras.add(jLabel6);

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("jLabel10");
        painelPalavras.add(jLabel10);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("testezaaaoo");
        painelPalavras.add(jLabel2);

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("jLabel7");
        painelPalavras.add(jLabel7);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("jLabel8");
        painelPalavras.add(jLabel8);

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("jLabel9");
        painelPalavras.add(jLabel9);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(painelPalavras, gridBagConstraints);

        labelGambiarra.setText("____________________________________");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(labelGambiarra, gridBagConstraints);

        labelPunicao.setFont(new java.awt.Font("Dialog", 0, 72)); // NOI18N
        labelPunicao.setForeground(new java.awt.Color(255, 0, 0));
        labelPunicao.setText("+10");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(20, 20, 20, 20);
        add(labelPunicao, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel labelContador;
    private javax.swing.JLabel labelGambiarra;
    private javax.swing.JLabel labelPunicao;
    private javax.swing.JLabel labelTema;
    private javax.swing.JPanel painelPalavras;
    // End of variables declaration//GEN-END:variables


    public void riscar(String palavra){
        
        for(int i = 0; i < labels.size(); i++){
            if(labels.get(i).getText().equals(palavra)){
                Map atributos = labels.get(i).getFont().getAttributes();
                atributos.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
                labels.get(i).setForeground(Color.red);
                labels.get(i).setFont(new Font(atributos));
            }
        }
        
        qtdePalavrasAchadas++;
        if(qtdePalavrasAchadas == 1){
            labelContador.setText("Você achou 1 palavra de " + qtdePalavras);
        } else {
            labelContador.setText("Você achou " + qtdePalavrasAchadas + " palavras de " + qtdePalavras);
        }            
    }

    public int getQtdePalavrasAchadas() {
        return qtdePalavrasAchadas;
    }
    
    
}
